/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Prison.java
 *
 * Created on 13-mrt-2010, 14:26:50
 */
package prison.gui;

import java.sql.SQLException;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import prison.data.*;
import prison.dataaccess.*;

/**
 *
 * @author joli
 */
public class Prison extends javax.swing.JFrame {

    User Prisoner;
    Locatie locatiePrisoner;

    /** Creates new form Prison */
    public Prison() {
        initComponents();
        LookAndFeelInstellen();
        enableDisable(false);
        loginRegister(false);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlLogin = new javax.swing.JPanel();
        bLogin = new javax.swing.JButton();
        txtUsername = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtPass = new javax.swing.JPasswordField();
        lblVerkeerd = new javax.swing.JLabel();
        lblEmail = new javax.swing.JLabel();
        txtEmail = new javax.swing.JTextField();
        lblPassCheck = new javax.swing.JLabel();
        txtPass2 = new javax.swing.JTextField();
        btnRegister = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtConsole = new javax.swing.JTextArea();
        txtCommand = new javax.swing.JTextField();
        bExecuteCommand = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        pnlLogin.setName("pnlLogin"); // NOI18N

        bLogin.setText("Go to prison");
        bLogin.setName("bLogin"); // NOI18N
        bLogin.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                bLoginMouseClicked(evt);
            }
        });

        txtUsername.setName("txtUsername"); // NOI18N

        jLabel1.setText("Username");
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText("Password");
        jLabel2.setName("jLabel2"); // NOI18N

        txtPass.setName("txtPass"); // NOI18N

        lblVerkeerd.setForeground(new java.awt.Color(255, 0, 0));
        lblVerkeerd.setText("Gebruikersnaam / Wachtwoord verkeerd");
        lblVerkeerd.setName("lblVerkeerd"); // NOI18N

        lblEmail.setText("E-mail");
        lblEmail.setName("lblEmail"); // NOI18N

        txtEmail.setName("txtEmail"); // NOI18N

        lblPassCheck.setText("Pass check");
        lblPassCheck.setName("lblPassCheck"); // NOI18N

        txtPass2.setName("txtPass2"); // NOI18N

        btnRegister.setText("Register");
        btnRegister.setName("btnRegister"); // NOI18N

        javax.swing.GroupLayout pnlLoginLayout = new javax.swing.GroupLayout(pnlLogin);
        pnlLogin.setLayout(pnlLoginLayout);
        pnlLoginLayout.setHorizontalGroup(
            pnlLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlLoginLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(lblEmail))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtEmail)
                    .addComponent(txtUsername, javax.swing.GroupLayout.DEFAULT_SIZE, 86, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(pnlLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(lblPassCheck))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtPass2)
                    .addComponent(txtPass, javax.swing.GroupLayout.DEFAULT_SIZE, 109, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btnRegister, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(bLogin, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addComponent(lblVerkeerd)
                .addContainerGap(24, Short.MAX_VALUE))
        );
        pnlLoginLayout.setVerticalGroup(
            pnlLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlLoginLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bLogin)
                    .addComponent(lblVerkeerd))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(pnlLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblEmail)
                    .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblPassCheck)
                    .addComponent(txtPass2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnRegister))
                .addContainerGap())
        );

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        txtConsole.setColumns(20);
        txtConsole.setEditable(false);
        txtConsole.setRows(5);
        txtConsole.setName("txtConsole"); // NOI18N
        jScrollPane1.setViewportView(txtConsole);

        txtCommand.setName("txtCommand"); // NOI18N

        bExecuteCommand.setText("Execute command");
        bExecuteCommand.setName("bExecuteCommand"); // NOI18N
        bExecuteCommand.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                bExecuteCommandMouseClicked(evt);
            }
        });
        bExecuteCommand.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                bExecuteCommandKeyPressed(evt);
            }
        });

        jLabel3.setText("GO");
        jLabel3.setName("jLabel3"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtCommand, javax.swing.GroupLayout.PREFERRED_SIZE, 456, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bExecuteCommand, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(pnlLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 664, Short.MAX_VALUE))))
                .addGap(326, 326, 326))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(pnlLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(11, 11, 11)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 504, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtCommand, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bExecuteCommand)
                    .addComponent(jLabel3))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bLoginMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_bLoginMouseClicked
        UserLogin();
    }//GEN-LAST:event_bLoginMouseClicked

    private void bExecuteCommandMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_bExecuteCommandMouseClicked
        uitvoerenCommando();
    }//GEN-LAST:event_bExecuteCommandMouseClicked

    private void bExecuteCommandKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_bExecuteCommandKeyPressed
        // TODO add your handling code here      
    }//GEN-LAST:event_bExecuteCommandKeyPressed

    ////////Algemeen
    private void LookAndFeelInstellen() {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            SwingUtilities.updateComponentTreeUI(this);
        } catch (Exception ex) {
        }
    }

    private void enableDisable(Boolean b) {
        lblVerkeerd.setVisible(b);
        txtConsole.enable(b);
        bExecuteCommand.enable(b);
        txtConsole.enable(b);
    }

    private void loginRegister(Boolean b){
        txtEmail.setVisible(b);
        txtPass2.setVisible(b);
        lblEmail.setVisible(b);
        lblPassCheck.setVisible(b);
        btnRegister.setVisible(b);
    }

    ///Inlog events
    private void UserLogin() {
        try {
            UserDAO dao = UserDAO.getInstance();
            Prisoner = dao.getUsers(txtUsername.getText(), txtPass.getText());

            //Controle ofdat de gebruiker wel bestaat
            if (Prisoner == null) {
                lblVerkeerd.setVisible(true);
                txtUsername.setText("");
                txtPass.setText("");
            } else {
                pnlLogin.setVisible(false);
                enableDisable(true);
                lblVerkeerd.setVisible(false);
                locatieOpvragenEnOptiesTonen(1);
            }
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
            ex.printStackTrace();
        }
    }

    //Begin Gebruiker verplaatsing
    private void locatieOpvragenEnOptiesTonen(int plaatsId) {

        try {
            MapDAO dao = MapDAO.getInstance();

            String sPlaats = Integer.toString(plaatsId);
            locatiePrisoner = dao.getLocatieViaId(sPlaats);

            txtConsole.append("U bent hier: " + locatiePrisoner.getPlaatsNaam() + "\n");

            uitwegenOpsommen();

        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
            ex.printStackTrace();
        }

    }

    private void uitwegenOpsommen() {
        Vector<Locatie> uitwegen = new Vector<Locatie>();
        try {
            MapDAO dao = MapDAO.getInstance();
            //uitwegen uit de database halen wel in formaat 2;4;7;9;
            uitwegen = dao.getUitwegen(locatiePrisoner.getUitweg().toString());

            System.out.println(uitwegen);
            String opsommingUitwegen = new String();

            for (Locatie uitweg : uitwegen) {
                opsommingUitwegen += uitweg.getPlaatsNaam() + ", ";
            }

            //Uitwegen opsommen en de laatste komma eruit zwieren
            txtConsole.append(opsommingUitwegen.substring(0, opsommingUitwegen.length() - 2) + "\n \n");

        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
            ex.printStackTrace();
        }

    }

    //Einde Gebruiker verplaatsing
    //Begin Uitvoeren en filteren van commando's
    private void uitvoerenCommando() {
        String commando = new String();
        commando = txtCommand.getText().toLowerCase();

        if (commando == null) {
            commando = "abcdefghijklmnopqrstuvwxyz";
        }

        if (commando.length() > 2) {
            if (commando.substring(0, 2).equals("go")) {
                String plaats = commando.substring(3, commando.length());

                try {
                    MapDAO dao = MapDAO.getInstance();
                    System.out.println(plaats);

                    int locatieCommando = dao.getLocatieViaNaam(plaats).getPlaatsId();

                    //Het veld leegmaken voor nieuw commando
                    txtCommand.setText("");

                    locatieOpvragenEnOptiesTonen(locatieCommando);
                } catch (SQLException ex) {
                    txtConsole.append("Onbestaande plaats");
                }


            }
        }

        if (commando.length() > 4) {
            if (commando.substring(0, 4).equals("exit")) {
                txtConsole.append(Prisoner.getNickname() + " is uitgelogd.");
                System.out.println("uitgelogd");
            }
        }
    }

    //Einde Uitvoeren en filteren van commando's
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Prison().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bExecuteCommand;
    private javax.swing.JButton bLogin;
    private javax.swing.JButton btnRegister;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblEmail;
    private javax.swing.JLabel lblPassCheck;
    private javax.swing.JLabel lblVerkeerd;
    private javax.swing.JPanel pnlLogin;
    private javax.swing.JTextField txtCommand;
    private javax.swing.JTextArea txtConsole;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JPasswordField txtPass;
    private javax.swing.JTextField txtPass2;
    private javax.swing.JTextField txtUsername;
    // End of variables declaration//GEN-END:variables
}
